/*
	构造restful基础框架类
*/
package data

import (
	"data/config"
	"data/view/file"
	"data/view/question"
	"log"
	"net/http"
	"public/tools"

	"data/router"

	"github.com/ant0ine/go-json-rest/rest"
)

var api *rest.Api = nil

type ApiRoot struct {
}

func (*ApiRoot) OnCreat() {
	api = rest.NewApi()
	if config.OnIsDev() {
		api.Use(rest.DefaultDevStack...) //DefaultProdStack
	} else {
		api.Use(rest.DefaultProdStack...)
	}
	router, err := router.OnInitRoter(api)
	if err != nil {
		log.Fatal(err)
		panic(err)
	}
	api.SetApp(router)

	//创建模版解析 ,创建文件
	BuildTempRole()

	//http.HandleFunc("/apiserver/upload/", upload.M_upload.UpLoadOne) //添加post form
	//微信相关接口
	//	http.HandleFunc("/hotelserver/index.do", weixin.Index)
	http.HandleFunc(config.Url_host+"/api/v1/file/upload", file.O_file.Upload)       //统一上传文件
	http.HandleFunc(config.Url_host+"/api/v1/question/multi_add", question.MultiAdd) //批量添加题目

	//-------------------end

	http.Handle(config.Url_host+"/api/", http.StripPrefix(config.Url_host+"/api", api.MakeHandler())) //指定api默认路由
}

func BuildTempRole() {
	if len(config.File_host) > 0 {
		//文件路径
		http.Handle(config.Url_host+config.File_host+"/",
			http.StripPrefix(config.Url_host+config.File_host,
				http.FileServer(http.Dir(tools.GetCurrentDirectory()+"/file"))))
	}

	if len(config.Static_host[0]) > 0 {
		dir := config.Static_host[0]
		host := config.Static_host[1]
		//如果没有，创建文件
		tools.BuildDir(dir)
		//文件路径
		http.Handle(host+"/",
			http.StripPrefix(host+"/",
				http.FileServer(http.Dir(dir))))
	}
}
